//
//  HomeViewModel.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/3.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit

class HomeViewModel: NSObject,UITableViewDelegate,UITableViewDataSource {
    
    
    lazy var model:HomeModel = {
        
        return loadData() ?? nil
        }()!
    
    func loadData() -> HomeModel? {
        
        let path = Bundle.main.path(forResource: "HomeData", ofType: "json")
        // 2. 获取json文件里面的内容,NSData格式
        let jsonData=NSData(contentsOfFile: path!)
        // 3. 解析json内容
        
        do {
            let json = try JSONSerialization.jsonObject(with: jsonData! as Data, options: .allowFragments)
            
            if let result = json as? Dictionary<String, Any> {
                
                return HomeModel.deserialize(from: result["data"] as? Dictionary)
                
            }
            else
            {
                return nil
            }
        } catch  {
            
            print("解析失败")
            return nil
        }
        
    }
    
    
}

extension HomeViewModel
{
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section==0 {
            
            return 0.1
        }
        else
        {
            return 40.0;
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        if section==0 {
            
            return UIView()
        }
        else
        {
            
            let view:WZC_TableViewHeaderFooterView = tableView.dequeueReusableHeaderFooterView(withIdentifier: WZC_TableViewHeaderFooterView.identifier()) as! WZC_TableViewHeaderFooterView
            view.titleLable.font = BFont(19)
            view.backgroundColor = UIColor.hexColor(hexadecimal: "#f2f2f2")
            view.titleLable.text = "专属推荐";
            return view;
            
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            
            return model.homepage?.count ?? 0
        }
        else
        {
            return model.recommend?.count ?? 0
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            
            let floorModel:HomeFloorModel = model.homepage![indexPath.row]
            
            return floorModel.height
        
        }
        else
        {
            let floorModel:RecommendModel = model.recommend![indexPath.row]
            
            let count : Int = floorModel.data?.count ?? 0
            
            var row = 0;
            
            if count%2 == 0{

               row = count/2
            }
            else
            {
                row = count/2 + 1
            }
            
            return CGFloat(row*64) * 4.5 +  CGFloat(row*10 +  20)
        }
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            
            let floorModel:HomeFloorModel =
                model.homepage![indexPath.row]
            if floorModel.module_key == "banner"
            {
                let cell:HomeBannerTableViewCell = tableView.dequeueReusableCell(withIdentifier: HomeBannerTableViewCell.identifier(), for: indexPath) as! HomeBannerTableViewCell
                cell.model = floorModel
                return cell
            }
            else if floorModel.module_key == "kingkong_activity" {
                let cell:HomeTodayActivityTableViewCell = tableView.dequeueReusableCell(withIdentifier: HomeTodayActivityTableViewCell.identifier(), for: indexPath) as! HomeTodayActivityTableViewCell
                cell.model = floorModel
                return cell;
                
            }
            else if floorModel.module_key == "recommend" {
                let cell:HomeRecommendTableViewCell = tableView.dequeueReusableCell(withIdentifier: HomeRecommendTableViewCell.identifier(), for: indexPath) as! HomeRecommendTableViewCell
                cell.model = floorModel
                return cell;
                
            }
            else if floorModel.module_key == "crowd_funding" {
                let cell:Crowdfund_TableViewCell = tableView.dequeueReusableCell(withIdentifier: Crowdfund_TableViewCell.identifier(), for: indexPath) as! Crowdfund_TableViewCell
                cell.model = floorModel
                return cell;
                
            }
            else if floorModel.module_key == "single_image" {
                let cell:HomeSingleImgTableViewCell = tableView.dequeueReusableCell(withIdentifier: HomeSingleImgTableViewCell.identifier(), for: indexPath) as! HomeSingleImgTableViewCell
                cell.model = floorModel
                return cell;
            }
            else if floorModel.module_key == "plaza" {
                let cell:PlazaTableViewCell = tableView.dequeueReusableCell(withIdentifier: PlazaTableViewCell.identifier(), for: indexPath) as! PlazaTableViewCell
                cell.model = floorModel
                return cell;
                
            }
            else
            {
                let cell:HotSellTableViewCell = tableView.dequeueReusableCell(withIdentifier: HotSellTableViewCell.identifier(), for: indexPath) as! HotSellTableViewCell
                cell.model = floorModel
                return cell;
            }
        }
        else
        {
            
            let cell:HotRecommendTableViewCell = tableView.dequeueReusableCell(withIdentifier: HotRecommendTableViewCell.identifier(), for: indexPath) as! HotRecommendTableViewCell
            cell.model = model.recommend![indexPath.row]
            return cell
        }
    }
}
